package com.guagua.chat.base;
/**
 * 北京八度互联科技有限公司版权所有
 * Copyright (C) Badu Corporation. All Rights Reserved
 */
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.guagua.base.AuthFactory;
import com.guagua.base.AuthToken;
import com.guagua.base.UnauthorizedException;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.webwork.interceptor.ServletResponseAware;
import com.opensymphony.webwork.interceptor.SessionAware;
import com.opensymphony.xwork.ActionSupport;
/** ******************************************************************
* 类概要:action基类
* @author 郑国英
* @version 2012-3-20 下午8:00:00
*******************************************************************/
public class BaseAction extends ActionSupport implements SessionAware,
		ServletRequestAware, ServletResponseAware {

	
	protected final static Logger logger = LoggerFactory.getLogger(BaseAction.class);
    //session对象
	protected Map session; 
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	
	

	public void setSession(Map session) {
		this.session = session;
	}
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	public Map getSession() {
		return session;
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	
private AuthToken authToken;
	/**
	 * 函数说明：从Token中获取用户ID <br/>
	 * 
	 * @return 返回结果说明. （long）用户ID
	 */
	public long getUserId(){
		authToken = getAuthToken();
		return authToken.getUserID();
	}
	public AuthToken getAuthToken(){
		AuthToken token = null;
		try{
			token = AuthFactory.getAuthToken(request,response);
		}catch(UnauthorizedException ue){
			token = AuthFactory.getAnonymousAuthToken();
		}
		return token;
	}
	
	public AuthToken getAuthTokenClient() {
		if (authToken == null) {
			HttpServletRequest request;
			javax.servlet.http.HttpServletResponse response;
			try {
				request = ServletActionContext.getRequest();
				response = ServletActionContext.getResponse();
			} catch (Exception e) {
				authToken = AuthFactory.getAnonymousAuthToken();
				return authToken;
			}
			if (request == null || response == null) {
				authToken = AuthFactory.getAnonymousAuthToken();
			} else if ("true".equals(request
					.getAttribute("jive.useAdminAuthToken"))) {
				if (getSession().get("jive.admin.authToken") != null)
					authToken = (AuthToken) getSession().get(
							"jive.admin.authToken");
				if (authToken == null)
					try {
						authToken = AuthFactory.getAuthToken(request, response);
					} catch (UnauthorizedException ue) {
						authToken = AuthFactory.getAnonymousAuthToken();
					}
			} else {
				try {
					authToken = AuthFactory.getAuthToken(request, response);
				} catch (UnauthorizedException ue) {
					authToken = AuthFactory.getAnonymousAuthToken();
				}
			}
		}
		return authToken;
	}
	
	
}
